import Vue from 'vue'
import VueRouter from 'vue-router'
import HomeView from '../views/HomeView.vue'
import PhotoManage from '../views/PhotoMange/PhotoManage'
import PhotoDetail from '../views/PhotoDetail/PhotoDetail'
import TagManage from '../views/TagManage/TagManage'
import TagToPhoto from '../views/TagManage/TagToPhoto'
import Collection from '../views/CollectionManage/Collection'
import Process from '../views/process/Process'
import AccountSet from '../views/AccountSet'
Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'login',
    component: () => import('../views/Login.vue')
  },
  {
      path: '/index',
      name:'index',
    component: () => import('../views/Index.vue'),
    children: [
      {
        path: '',
        name: 'home',
        component: HomeView,
          meta: {
              title: '主页面',
              requireAuth: true
          }
      },
        {
            path: 'photoManage',
            name: 'photoManage',
            component: PhotoManage,
            meta: {
                title: '照片管理',
                requireAuth: true
            }
        },
        {
            path: 'photoDetail',
            name: 'photoDetail',
            component: PhotoDetail,
            meta: {
                title: '照片详情页面',
                requireAuth: true
            }
        },
        {
            path: 'tagManage',
            name: 'tagManage',
            component: TagManage,
            meta: {
                title: '标签集页面',
                requireAuth: true
            }
        },
        {
            path: 'tagToPhoto',
            name: 'tagToPhoto',
            component: TagToPhoto,
            meta: {
                title: '相同标签的照片页面',
                requireAuth: true
            }
        },
        {
            path: 'collection',
            name: 'collection',
            component: Collection,
            meta: {
                title: '收藏页面',
                requireAuth: true
            }
        },
        {
            path: 'process',
            name: 'process',
            component: Process,
            meta: {
                title: '照片处理页面',
                requireAuth: true
            }
        },
        {
            path: 'accountSet',
            name: 'accountSet',
            component: AccountSet,
            meta: {
                title: '注册页面',
                requireAuth: true
            }
        }
    ]
  },
  {
    path: '*',
    name: '404',
    component: () => import('../views/404.vue')
  }
]

const router = new VueRouter({
  routes
})

router.beforeEach((to, from, next) => {
    document.title = to.matched[0].meta.title; // 显示标题
    // next()
    if (to.meta.requireAuth) { // 判断该路由是否需要登录权限
        if (window.sessionStorage.getItem('isLogin') === 'true') {
            next()
        } else {
            // 未登录，跳转到登录页面
            next('/')
        }
    } else {
        next()
    }
})
export default router
